package jsp;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet瀹炵幇绫籉indServlet
 */
@WebServlet("/")	//閰嶇疆Servlet涓洪粯璁ゆ墽琛岄〉
public class FindServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public FindServlet() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int currPage = 1; 										// 褰撳墠椤电爜
		if(request.getParameter("page") != null){ 				// 鍒ゆ柇浼犻�掗〉鐮佹槸鍚︽湁鏁�
			currPage = Integer.parseInt(request.getParameter("page"));// 瀵瑰綋鍓嶉〉鐮佽祴鍊�
		}
		BookDao dao = new BookDao();					// 瀹炰緥鍖朆ookDao
		List<BookBean> list = dao.find(currPage);			// 鏌ヨ鎵�鏈夊浘涔︿俊鎭�
		request.setAttribute("list", list);				// 灏唋ist鏀剧疆鍒皉equest涓�
		int pages ; 											// 鎬婚〉鏁�
		int count = dao.findCount();							// 鏌ヨ鎬昏褰曟暟
		if(count % BookBean.PAGE_SIZE == 0){ 					// 璁＄畻鎬婚〉鏁�
			pages = count / BookBean.PAGE_SIZE; 				// 瀵规�婚〉鏁拌祴鍊�
		}else{
			pages = count / BookBean.PAGE_SIZE + 1; 				// 瀵规�婚〉鏁拌祴鍊�
		}
		StringBuffer sb = new StringBuffer();					// 瀹炰緥鍖朣tringBuffer
		for(int i=1; i <= pages; i++){							// 閫氳繃寰幆鏋勫缓鍒嗛〉瀵艰埅鏉�
			if(i == currPage){ 								// 鍒ゆ柇鏄惁涓哄綋鍓嶉〉
				sb.append("銆�" + i + "銆�");					// 鏋勫缓鍒嗛〉瀵艰埅鏉�
			}else{
				// 鏋勫缓鍒嗛〉瀵艰埅鏉�
				sb.append("<a href='FindServlet?page=" + i + "'>" + i + "</a>");
			}
			sb.append("銆�");									// 鏋勫缓鍒嗛〉瀵艰埅鏉�
		}
		request.setAttribute("bar", sb.toString());	// 灏嗗垎椤靛鑸潯鐨勫瓧绗︿覆鏀剧疆鍒皉equest涓�
		// 杞彂鍒癰ookList.jsp椤甸潰
		request.getRequestDispatcher("bookList.jsp").forward(request, response);

	}
}
